﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ShareContent;
using Microsoft.Xna.Framework;

namespace AncientEmpires_025_132
{
    public class NationSetupMenu : Menu
    {
        public delegate void EventHandleClick(object sender);
        public event EventHandleClick ClickButton;

        private NationSetup _nationSetup;

        public NationSetup NationSetup
        {
            get { return _nationSetup; }
            set { _nationSetup = value; }
        }

        private List<MyButton> listLeft, listRight;
        private Sprite2D player,cpu,none,team1,team2,team3,team4;

        private NationSetupMenuWithXML _nationSettingMenuWithXMl;

        public NationSetupMenu(ContentManager content, GraphicsDevice graphicsDevice, string strmenuxml, NationSetup nationSetup)
        {
            _nationSettingMenuWithXMl = content.Load<NationSetupMenuWithXML>(strmenuxml);
            _nationSetup = nationSetup;
            SizeScale = _nationSettingMenuWithXMl.sizeScale;
            Size = new Vector2(graphicsDevice.Viewport.Width * SizeScale.X, graphicsDevice.Viewport.Height * SizeScale.Y);
            // Load background
            switch (nationSetup.NationNameNationSetup)
            { 
                case NationName.Red:
                    Background = LoadStaticItem(content, _nationSettingMenuWithXMl.strBackgroundRed, _nationSettingMenuWithXMl.sizeScale, _nationSettingMenuWithXMl.positionScaleRed, "NationSetupMenu/",new Vector2(0,0));
                    break;
                case NationName.Black:
                    Background = LoadStaticItem(content, _nationSettingMenuWithXMl.strBackgroundBlack, _nationSettingMenuWithXMl.sizeScale, _nationSettingMenuWithXMl.positionScaleBlack, "NationSetupMenu/",new Vector2(0,0));
                    break;
                case NationName.Green:
                    Background = LoadStaticItem(content, _nationSettingMenuWithXMl.strBackgroundGreen, _nationSettingMenuWithXMl.sizeScale, _nationSettingMenuWithXMl.positionScaleGreen, "NationSetupMenu/",new Vector2(0,0));
                    break;
                case NationName.Blue:
                    Background = LoadStaticItem(content, _nationSettingMenuWithXMl.strBackgroundBlue, _nationSettingMenuWithXMl.sizeScale, _nationSettingMenuWithXMl.positionScaleBlue, "NationSetupMenu/",new Vector2(0,0));
                    break;
            }
            // Load static items
            player = LoadStaticItem(content, _nationSettingMenuWithXMl.strPlayer, _nationSettingMenuWithXMl.sizeScalePlayer, _nationSettingMenuWithXMl.positionScalePlayer, "NationSetupMenu/", Background.Position);
            cpu = LoadStaticItem(content, _nationSettingMenuWithXMl.strCpu, _nationSettingMenuWithXMl.sizeScaleCpu, _nationSettingMenuWithXMl.positionScaleCpu, "NationSetupMenu/", Background.Position);
            none = LoadStaticItem(content, _nationSettingMenuWithXMl.strNone, _nationSettingMenuWithXMl.sizeScaleNone, _nationSettingMenuWithXMl.positionScaleNone, "NationSetupMenu/", Background.Position);
            team1 = LoadStaticItem(content, _nationSettingMenuWithXMl.strTeam1, _nationSettingMenuWithXMl.sizeScaleTeam1, _nationSettingMenuWithXMl.positionScaleTeam1, "NationSetupMenu/", Background.Position);
            team2 = LoadStaticItem(content, _nationSettingMenuWithXMl.strTeam2, _nationSettingMenuWithXMl.sizeScaleTeam2, _nationSettingMenuWithXMl.positionScaleTeam2, "NationSetupMenu/", Background.Position);
            team3 = LoadStaticItem(content, _nationSettingMenuWithXMl.strTeam3, _nationSettingMenuWithXMl.sizeScaleTeam3, _nationSettingMenuWithXMl.positionScaleTeam3, "NationSetupMenu/", Background.Position);
            team4 = LoadStaticItem(content, _nationSettingMenuWithXMl.strTeam4, _nationSettingMenuWithXMl.sizeScaleTeam4, _nationSettingMenuWithXMl.positionScaleTeam4, "NationSetupMenu/", Background.Position);
            // Load button items
            listLeft = new List<MyButton>();
            listLeft = LoadListItem(content, _nationSettingMenuWithXMl.listLeft, "NationSetupMenu/", Background.Position);
            for (int i = 0; i < listLeft.Count; i++)
            {
                listLeft[i].ClickButtonWithSender += new MyButton.EventHandleClickWithSender(ClickButtonWithSenderLeft);
            }

            listRight = new List<MyButton>();
            listRight = LoadListItem(content, _nationSettingMenuWithXMl.listRight, "NationSetupMenu/", Background.Position);
            for (int i = 0; i < listRight.Count; i++)
            {
                listRight[i].ClickButtonWithSender += new MyButton.EventHandleClickWithSender(ClickButtonWithSenderRight);
            } 
        }

        public void UpdateMenuSize(GraphicsDevice graphicsDevice)
        {
            // Update menu size
            Size = new Vector2(graphicsDevice.Viewport.Width * SizeScale.X, graphicsDevice.Viewport.Height * SizeScale.Y);
            // Update background
            switch (_nationSetup.NationNameNationSetup)
            {
                case NationName.Red:
                    Background.Size = new Vector2(_nationSettingMenuWithXMl.sizeScale.X * Size.X, _nationSettingMenuWithXMl.sizeScale.Y * Size.Y);
                    Background.Position = new Vector2(_nationSettingMenuWithXMl.positionScaleRed.X * Size.X, _nationSettingMenuWithXMl.positionScaleRed.Y * Size.Y);
                    break;
                case NationName.Black:
                    Background.Size = new Vector2(_nationSettingMenuWithXMl.sizeScale.X * Size.X, _nationSettingMenuWithXMl.sizeScale.Y * Size.Y);
                    Background.Position = new Vector2(_nationSettingMenuWithXMl.positionScaleBlack.X * Size.X, _nationSettingMenuWithXMl.positionScaleBlack.Y * Size.Y);
                    break;
                case NationName.Green:
                    Background.Size = new Vector2(_nationSettingMenuWithXMl.sizeScale.X * Size.X, _nationSettingMenuWithXMl.sizeScale.Y * Size.Y);
                    Background.Position = new Vector2(_nationSettingMenuWithXMl.positionScaleGreen.X * Size.X, _nationSettingMenuWithXMl.positionScaleGreen.Y * Size.Y);
                    break;
                case NationName.Blue:
                    Background.Size = new Vector2(_nationSettingMenuWithXMl.sizeScale.X * Size.X, _nationSettingMenuWithXMl.sizeScale.Y * Size.Y);
                    Background.Position = new Vector2(_nationSettingMenuWithXMl.positionScaleBlue.X * Size.X, _nationSettingMenuWithXMl.positionScaleBlue.Y * Size.Y);
                    break;
            }
            // Update list left
            for (int i = 0; i < listLeft.Count; i++)
            {
                listLeft[i].SetPosition(new Vector2(_nationSettingMenuWithXMl.listLeft[i].positionScale.X * Size.X + Background.Position.X, _nationSettingMenuWithXMl.listLeft[i].positionScale.Y * Size.Y + Background.Position.Y));
                listLeft[i].SetSize(new Vector2(_nationSettingMenuWithXMl.listLeft[i].sizeScale.X * Size.X, _nationSettingMenuWithXMl.listLeft[i].sizeScale.Y * Size.Y));
            }
            // Update list right
            for (int i = 0; i < listRight.Count; i++)
            {
                listRight[i].SetPosition(new Vector2(_nationSettingMenuWithXMl.listRight[i].positionScale.X * Size.X + Background.Position.X, _nationSettingMenuWithXMl.listRight[i].positionScale.Y * Size.Y + Background.Position.Y));
                listRight[i].SetSize(new Vector2(_nationSettingMenuWithXMl.listRight[i].sizeScale.X * Size.X, _nationSettingMenuWithXMl.listRight[i].sizeScale.Y * Size.Y));
            }
            // Update player
            player.Size = new Vector2(_nationSettingMenuWithXMl.sizeScalePlayer.X * Size.X, _nationSettingMenuWithXMl.sizeScalePlayer.Y * Size.Y);
            player.Position = new Vector2(_nationSettingMenuWithXMl.positionScalePlayer.X * Size.X + Background.Position.X, _nationSettingMenuWithXMl.positionScalePlayer.Y * Size.Y + Background.Position.Y);

            // Update cpu
            cpu.Size = new Vector2(_nationSettingMenuWithXMl.sizeScaleCpu.X * Size.X, _nationSettingMenuWithXMl.sizeScaleCpu.Y * Size.Y);
            cpu.Position = new Vector2(_nationSettingMenuWithXMl.positionScaleCpu.X * Size.X + Background.Position.X, _nationSettingMenuWithXMl.positionScaleCpu.Y * Size.Y + Background.Position.Y);

            // Update none
            none.Size = new Vector2(_nationSettingMenuWithXMl.sizeScaleNone.X * Size.X, _nationSettingMenuWithXMl.sizeScaleNone.Y * Size.Y);
            none.Position = new Vector2(_nationSettingMenuWithXMl.positionScaleNone.X * Size.X + Background.Position.X, _nationSettingMenuWithXMl.positionScaleNone.Y * Size.Y + Background.Position.Y);
            // Update team
            team1.Size = new Vector2(_nationSettingMenuWithXMl.sizeScaleTeam1.X * Size.X, _nationSettingMenuWithXMl.sizeScaleTeam1.Y * Size.Y);
            team1.Position = new Vector2(_nationSettingMenuWithXMl.positionScaleTeam1.X * Size.X + Background.Position.X, _nationSettingMenuWithXMl.positionScaleTeam1.Y * Size.Y + Background.Position.Y);

            team2.Size = new Vector2(_nationSettingMenuWithXMl.sizeScaleTeam2.X * Size.X, _nationSettingMenuWithXMl.sizeScaleTeam2.Y * Size.Y);
            team2.Position = new Vector2(_nationSettingMenuWithXMl.positionScaleTeam2.X * Size.X + Background.Position.X, _nationSettingMenuWithXMl.positionScaleTeam2.Y * Size.Y + Background.Position.Y);

            team3.Size = new Vector2(_nationSettingMenuWithXMl.sizeScaleTeam3.X * Size.X, _nationSettingMenuWithXMl.sizeScaleTeam3.Y * Size.Y);
            team3.Position = new Vector2(_nationSettingMenuWithXMl.positionScaleTeam3.X * Size.X + Background.Position.X, _nationSettingMenuWithXMl.positionScaleTeam3.Y * Size.Y + Background.Position.Y);

            team4.Size = new Vector2(_nationSettingMenuWithXMl.sizeScaleTeam4.X * Size.X, _nationSettingMenuWithXMl.sizeScaleTeam4.Y * Size.Y);
            team4.Position = new Vector2(_nationSettingMenuWithXMl.positionScaleTeam4.X * Size.X + Background.Position.X, _nationSettingMenuWithXMl.positionScaleTeam4.Y * Size.Y + Background.Position.Y);
           
        }

        public void ClickButtonWithSenderLeft(object sender)
        {
            int choice = listLeft.IndexOf((MyButton)sender);
            switch (choice)
            { 
                case 0:
                    NationSetup.Update(NationSetup.TypeUpdate.UpdatePlayer, NationSetup.Navigate.Back);
                    break;
                case 1:
                    NationSetup.Update(NationSetup.TypeUpdate.UpdateTeam, NationSetup.Navigate.Back);
                    break;
            }
            ClickButton(this);
        }

        public void ClickButtonWithSenderRight(object sender)
        {
            int choice = listRight.IndexOf((MyButton)sender);
            switch (choice)
            {
                case 0:
                    NationSetup.Update(NationSetup.TypeUpdate.UpdatePlayer, NationSetup.Navigate.Next);
                    break;
                case 1:
                    NationSetup.Update(NationSetup.TypeUpdate.UpdateTeam, NationSetup.Navigate.Next);
                    break;
            }
            ClickButton(this);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            // Draw background
            Background.Draw(gameTime, spriteBatch);
            // Draw player
            switch (NationSetup.TypeNationSetup)
            { 
                case TypeNation.Player:
                    player.Draw(gameTime, spriteBatch);
                    break;
                case TypeNation.Computer:
                    cpu.Draw(gameTime, spriteBatch);
                    break;
                case TypeNation.None:
                    none.Draw(gameTime, spriteBatch);
                    break;
            }
            // Draw team
            switch (NationSetup.TeamNameSetup)
            { 
                case TeamName.Team1:
                    team1.Draw(gameTime, spriteBatch);
                    break;
                case TeamName.Team2:
                    team2.Draw(gameTime, spriteBatch);
                    break;
                case TeamName.Team3:
                    team3.Draw(gameTime, spriteBatch);
                    break;
                case TeamName.Team4:
                    team4.Draw(gameTime, spriteBatch);
                    break;
            }
            for (int i = 0; i < listLeft.Count; i++)
            {
                listLeft[i].Draw(gameTime, spriteBatch);
            }
            for (int i = 0; i < listRight.Count; i++)
            {
                listRight[i].Draw(gameTime, spriteBatch);
            }

        }

        public override void Update(GameTime gametime)
        {
            // Update Background
            Background.Update(gametime);
            // Draw player
            switch (NationSetup.TypeNationSetup)
            {
                case TypeNation.Player:
                    player.Update(gametime);
                    break;
                case TypeNation.Computer:
                    cpu.Update(gametime);
                    break;
                case TypeNation.None:
                    none.Update(gametime);
                    break;
            }
            // Draw team
            switch (NationSetup.TeamNameSetup)
            {
                case TeamName.Team1:
                    team1.Update(gametime);
                    break;
                case TeamName.Team2:
                    team2.Update(gametime);
                    break;
                case TeamName.Team3:
                    team3.Update(gametime);
                    break;
                case TeamName.Team4:
                    team4.Update(gametime);
                    break;
            }
            for (int i = 0; i < listLeft.Count; i++)
            {
                listLeft[i].Update(gametime);
            }
            for (int i = 0; i < listRight.Count; i++)
            {
                listRight[i].Update(gametime);
            }
        }
    }
}
